package com.hunttown.mes.impl;

import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.convert.AnalysisRegionConverts;
import com.hunttown.mes.rpc.domain.AnalysisRegionDTO;
import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.rpc.api.AnalysisRegionRpcService;
import com.hunttown.mes.rpc.domain.query.AnalysisRegionDTOQuery;
import com.hunttown.mes.service.AnalysisRegionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.AnalysisRegionConverts.convert;

/**
 * created by wangjunfu on 2018-11-26 15:06:34
 */
@Service
public class AnalysisRegionRpcServiceImpl implements AnalysisRegionRpcService {

    private final Logger logger = LoggerFactory.getLogger(AnalysisRegionRpcServiceImpl.class);

    private final AnalysisRegionService analysisRegionService;

    @Autowired
    public AnalysisRegionRpcServiceImpl(AnalysisRegionService analysisRegionService) {
        this.analysisRegionService = analysisRegionService;
    }

    @Override
    public AnalysisRegionDTO insert(AnalysisRegionDTO analysisRegionDTO) {
        try {
            return convert(analysisRegionService.insert(AnalysisRegionConverts.convert(analysisRegionDTO)));
        } catch (Exception e) {
            logger.error("AnalysisRegion create failed.", e);
            throw new RuntimeException("AnalysisRegion create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(AnalysisRegionDTO analysisRegionDTO) {
        try {
            return analysisRegionService.updateInfoById(AnalysisRegionConverts.convert(analysisRegionDTO));
        } catch (Exception e) {
            logger.error("AnalysisRegion update by id failed.", e);
            throw new RuntimeException("AnalysisRegion update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(AnalysisRegionDTOQuery analysisRegionDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (analysisRegionDTOQuery.getW_id() != null && analysisRegionDTOQuery.getW_id() > 0) {
                query.put("w_id", analysisRegionDTOQuery.getW_id());
            }

            if (analysisRegionDTOQuery.getIds() != null && !analysisRegionDTOQuery.getIds().equals("")) {
                query.put("ids", analysisRegionDTOQuery.getIds());
            }

            return analysisRegionService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("AnalysisRegion update by query failed.", e);
            throw new RuntimeException("AnalysisRegion update by query failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisRegionDTO selectById(Integer id) {
        try {
            return convert(analysisRegionService.selectById(id));
        } catch (Exception e) {
            logger.error("AnalysisRegion select by id failed.", e);
            throw new RuntimeException("AnalysisRegion select by id failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisRegionDTO selectObjByQuery(AnalysisRegionDTOQuery analysisRegionDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (StringUtils.isNotBlank(analysisRegionDTOQuery.getRegionCode())) {
                query.put("regionCode", analysisRegionDTOQuery.getRegionCode());
            }

            return convert(analysisRegionService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("AnalysisRegion select obj by query failed.", e);
            throw new RuntimeException("AnalysisRegion select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<AnalysisRegionDTO> getListForPage(AnalysisRegionDTOQuery analysisRegionDTOQuery) {
        Query query = Query.page(analysisRegionDTOQuery.getPn(), analysisRegionDTOQuery.getPageSize());

        if (analysisRegionDTOQuery.getIds() != null && !analysisRegionDTOQuery.getIds().equals("")) {
            query.put("ids", analysisRegionDTOQuery.getIds());
        }

        if (analysisRegionDTOQuery.getLikeName() != null && !analysisRegionDTOQuery.getLikeName().equals("")) {
            query.put("likeName", analysisRegionDTOQuery.getLikeName());
        }

        if (analysisRegionDTOQuery.getParentId() != null) {
            query.put("parentId", analysisRegionDTOQuery.getParentId());
        }

        if (analysisRegionDTOQuery.getBeforeId() != null) {
            query.put("beforeId", analysisRegionDTOQuery.getBeforeId());
        }

        if (analysisRegionDTOQuery.getAfterId() != null) {
            query.put("afterId", analysisRegionDTOQuery.getAfterId());
        }

        //parent_id是否存在：1存在，0不存在
        if (analysisRegionDTOQuery.getParentExist() != null) {
            if (analysisRegionDTOQuery.getParentExist() == 1) {
                query.put("parentExist", analysisRegionDTOQuery.getParentExist());
            } else {
                query.put("parentNotExist", analysisRegionDTOQuery.getParentExist());
            }
        }

        if (analysisRegionDTOQuery.getOrderBy() != null && analysisRegionDTOQuery.getOrderBy().length() > 0) {
            query.put("orderBy", analysisRegionDTOQuery.getOrderBy());
        }

        List<AnalysisRegionDTO> list = convert(analysisRegionService.selectByQuery(query));
        Page<AnalysisRegionDTO> page = Page.from(analysisRegionDTOQuery.getPn(), analysisRegionDTOQuery.getPageSize(), list);
        page.setTotalItems(analysisRegionService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return analysisRegionService.deleteById(id);
        } catch (Exception e) {
            logger.error("AnalysisRegion delete failed.", e);
            throw new RuntimeException("AnalysisRegion delete failed, Please order your code.", e);
        }
    }
}